package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.bean.CountDownTime;
import com.epfresh.bean.LimitedTimeSpecialBean;
import com.epfresh.bean.NewHomeBean;
import com.epfresh.views.HomeCountDownView;

import java.util.List;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/4/22
 * Desc:
 */
public class LimitedTimeSpecialViewBinder extends ItemViewBinder<LimitedTimeSpecialBean, LimitedTimeSpecialViewBinder.ViewHolder> {
    private boolean isInit = true;
    private OnItemClickListener onItemClickListener;
    private CountDownTime cdt = new CountDownTime();
    private HomeCountDownView.OnCountDownListener countDownListener;

    public void setCountDownListener(HomeCountDownView.OnCountDownListener countDownListener) {
        this.countDownListener = countDownListener;
    }

    public void setInit(boolean init) {
        isInit = init;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_limited_time_special, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final LimitedTimeSpecialBean item) {
        if (isInit) {
            holder.txt_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onMoreClick(item);
                    }
                }
            });

            holder.rv_special_list.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext(),RecyclerView.HORIZONTAL,false));
            if (item.getSpikeBeanList().size() == 1){
                holder.adapterSingle = new LimitedTimeSpecialAdapterSingle();
                holder.rv_special_list.setAdapter(holder.adapterSingle);
                holder.setGoodsDataSingle(item.getSpikeBeanList());
                holder.adapterSingle.setOnLimitedTimeGoodClickListener(new LimitedTimeSpecialAdapterSingle.OnLimitedTimeGoodClickListener() {
                    @Override
                    public void onLimitedTimeGoodClick(NewHomeBean.SpikeBean spikeBean) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onLimitedTimeGoodClick(spikeBean);
                        }
                    }
                });

            }else {
                holder.adapter = new LimitedTimeSpecialAdapter();
                holder.rv_special_list.setAdapter(holder.adapter);
                holder.setGoodsData(item.getSpikeBeanList(),item.getSeckillTotal());
                holder.adapter.setOnLimitedTimeGoodClickListener(new LimitedTimeSpecialAdapter.OnLimitedTimeGoodClickListener() {
                    @Override
                    public void onLimitedTimeGoodClick(NewHomeBean.SpikeBean spikeBean) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onLimitedTimeGoodClick(spikeBean);
                        }
                    }

                    @Override
                    public void showMore() {
                        if (onItemClickListener != null) {
                            onItemClickListener.onMoreClick(item);
                        }
                    }
                });
            }
            isInit = false;
        }
        cdt.setStartSubTime(item.getSeckillBeginTime());
        cdt.setEndSubTime(item.getSeckillEndTime());
        cdt.setSysDate(item.getSysDate());
        cdt.init();

        holder.home_count_down.setOnCountDownListener(countDownListener);
        long countTime = cdt.getCountDownTime();
        if (countTime == 0) {//活动已经结束

        } else if (countTime > 0) {//活动进行中
            holder.home_count_down.showTimer(cdt, countTime, 1000);
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        private HomeCountDownView home_count_down;
        private TextView txt_more;
        private RecyclerView rv_special_list;
        private LimitedTimeSpecialAdapter adapter;
        private LimitedTimeSpecialAdapterSingle adapterSingle;

        ViewHolder(View itemView) {
            super(itemView);

            home_count_down = itemView.findViewById(R.id.home_count_down);
            txt_more = itemView.findViewById(R.id.txt_more);
            rv_special_list = itemView.findViewById(R.id.rv_special_list);


        }

        private void setGoodsData(List<NewHomeBean.SpikeBean> goods,String seckillTotal) {
            adapter.setGoods(goods,seckillTotal);
        }

        private void setGoodsDataSingle(List<NewHomeBean.SpikeBean> goods) {
            adapterSingle.setGoods(goods);
        }
    }

    public interface OnItemClickListener {
        void onMoreClick(LimitedTimeSpecialBean item);
        void onLimitedTimeGoodClick(NewHomeBean.SpikeBean spikeBean);
    }


}
